##
## Makefile for KyleCache. Based on intel template found in /pin/source/tools/MyPinTool
##
## Pin is required to build this program.  You can download it from pintool.org.
##
## This makefile links in the pin library when compiling the main tool object file and 
## when linking.
##
## The secondary source files (classes, etc) are simply built with icc (no linking) and linked later.
##
## First, point this to your pin installation

PIN_HOME ?= pin

##
## To build the tool, execute the make command:
##
##      make
##
## After building your tool, you would invoke Pin like this:
## 
##      $PIN_HOME/pin -t MyPinTool -- /bin/ls
##

##############################################################
#
# set up and include *.config files
#
##############################################################

PIN_KIT=$(PIN_HOME)
KIT=1
TESTAPP=$(OBJDIR)cp-pin.exe

TARGET_COMPILER?=gnu
ifdef OS
    ifeq (${OS},Windows_NT)
        TARGET_COMPILER=ms
    endif
endif

ifeq ($(TARGET_COMPILER),gnu)
    include $(PIN_HOME)/source/tools/makefile.gnu.config
    CXXFLAGS ?=-Wall -Werror -Wno-unknown-pragmas $(DBG)
     #$(OPT) took this off
    PIN=$(PIN_HOME)/pin
endif

ifeq ($(TARGET_COMPILER),ms)
    include $(PIN_HOME)/source/tools/makefile.ms.config
    DBG?=
    PIN=$(PIN_HOME)/pin.bat
endif


##############################################################
#
# File specifications:
#
##############################################################


TOOL_ROOTS = KyleCache 

#Need to include a .o file for each new .cpp file here and only here.


REQS = $(addprefix $(OBJDIR)o/,Cache.o ReplacementPolicy.o )

TOOLS = $(TOOL_ROOTS:%=$(OBJDIR)%$(PINTOOL_SUFFIX))


##############################################################
#
# build rules
#
##############################################################

all: tools
tools: $(OBJDIR) $(TOOLS) $(OBJDIR)cp-pin.exe
test: $(OBJDIR) $(TOOL_ROOTS:%=%.test)

KyleCache.test: $(OBJDIR)cp-pin.exe
	$(MAKE) -k PIN_HOME=$(PIN_HOME)

$(OBJDIR)cp-pin.exe:
	$(CXX) $(PIN_HOME)/source/tools/Tests/cp-pin.cpp $(APP_CXXFLAGS) -fPIC -o $(OBJDIR)cp-pin.exe

$(OBJDIR):
	mkdir -p $(OBJDIR)o/

%.cpp:%.h

$(OBJDIR)%.o : %.cpp 
	$(CXX) -c $(CXXFLAGS) $(PIN_CXXFLAGS) ${OUTOPT} $@ $<


$(OBJDIR)o/%.o: %.cpp 
	$(CXX) -c $(CXXFLAGS) $(PIN_CXXFLAGS) ${OUTOPT} $@ $<

#	g++ -c -FPIC -o $@ $<


$(TOOLS): $(PIN_LIBNAMES)

$(TOOLS): %$(PINTOOL_SUFFIX): %.o $(REQS)	
	${PIN_LD} -fPIC $(PIN_LDFLAGS) $(LINK_DEBUG) ${LINK_OUT}$@ $^ ${PIN_LPATHS} $(PIN_LIBS) $(DBG)


## cleaning
clean:
	-rm -rf $(OBJDIR) *.out *.tested *.failed makefile.copy
